package MyLinkList;

public class MyLinkList implements Ilist{
    //节点的数据结构
    static class ListNode{
        public int val;
        public ListNode next;
        ListNode(int val) {
            this.val = val;
        }
    }
    //链表的属性
    public ListNode head;//头节点
    //初始化链表，放进部分元素
    public void initList(){
        ListNode node1 = new ListNode(10);
        ListNode node2 = new ListNode(20);
        ListNode node3 = new ListNode(30);
        ListNode node4 = new ListNode(40);
        ListNode node5 = new ListNode(50);
        
        this.head = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
    }
    
    @Override//头插法
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        newNode.next = this.head;
        this.head = newNode;
    }
    
    @Override//尾插法
    public void addLast(int data) {
        //当链表为空的时候，直接将节点插入到头节点的位置
        ListNode newNode = new ListNode(data);
        if (head == null) {
            head = newNode;
        }else{
            ListNode cur = head;
            while (cur.next != null){
                cur = cur.next;
            }
            //找到最后一个节点
            cur.next = newNode;
            //newNode.next = null;//默认新节点的指针域为空，所以这里可以不写这一行代码
        }
    }
    
    @Override//指定位置添加
    public void addIndex(int index, int data) {
        if(index < 0 || index > size()) {
            throw new IndexException("index不合法: "+index);
        }
        //如果输入的位置在最前面就进行头插
        if (index == 0)
            addFirst(data);
        //如果输入的位置在链表最后就进行尾插
        if (index == size())
            addLast(data);
        //输入位置在中间，就先找到这个节点的位置
        ListNode cur = searchPrevIndex(index);
        ListNode newNode = new ListNode(data);
        //这俩步是顺序非常重要
        newNode.next = cur.next;
        cur.next = newNode;
    }
    //找到位置对应的节点
    private ListNode searchPrevIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index-2) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    
    @Override//查找是否存在
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key)
                return true;
            cur = cur.next;
        }
        return false;
    }
    
    @Override//删除第一个关键字
    public void remove(int key) {
        if (head == null)
            return;
        if (head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = findKey(key);
        if (cur == null){
            System.out.println("查无此元素，无法删除");
            return;
        }
        ListNode delNode = cur.next;
        cur.next = delNode.next;
        delNode.next = null;
    }
    //找到要删除的元素的前一个节点
    private ListNode findKey(int key){
        ListNode cur = head;
        while (cur.next != null){
            if (cur.next.val != key) {
                cur = cur.next;
            }else{
                return cur;
            }
        }
        return null;
    }
    
    @Override//删除所有的关键字
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        
        while (cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //除了头节点都删除完成了
        if(head.val == key) {
            head = head.next;
        }
    }
    
    @Override//求链表的长度
    public int size() {
        int count =0;
        ListNode cur = head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    
    @Override//清空链表
    public void clear() {
        head = null;
    }
    
    @Override//遍历展示链表
    public void display() {
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
